package com.wt.struggle.goodbye.base

import android.content.Context
import android.location.Location

class Share {
    companion object {

        val USER: String = "user"
        val SET: String = "set"

        fun saveToken(context: Context, token: String) {
            val edit = context.getSharedPreferences(USER, Context.MODE_PRIVATE).edit()
            edit.putString("token", token)
            edit.apply()
        }

        fun getToken(context: Context) = context.getSharedPreferences(USER, Context.MODE_PRIVATE).getString("token", "")


        fun clearToken(context: Context) {
            val edit = context.getSharedPreferences(USER, Context.MODE_PRIVATE).edit()
            edit.putString("token", "")
            edit.apply()
        }


        fun saveUid(context: Context, uid: Int) {
            val edit = context.getSharedPreferences(USER, Context.MODE_PRIVATE).edit()
            edit.putInt("userId", uid)
            edit.apply()
        }

        fun getId(context: Context) = context.getSharedPreferences(USER, Context.MODE_PRIVATE).getInt("userId", 0)


        fun saveAccount(context: Context, account: String) {
            val edit = context.getSharedPreferences(USER, Context.MODE_PRIVATE).edit()
            edit.putString("account", account)
            edit.apply()
        }


        fun getAccount(context: Context) =
            context.getSharedPreferences(USER, Context.MODE_PRIVATE).getString("account", "")


        fun saveTextSize(context: Context, size: Float) {
            val edit = context.getSharedPreferences(SET, Context.MODE_PRIVATE).edit()
            edit.putFloat("size", size)
            edit.apply()
        }


        fun saveUuid(context: Context, uuid: String?) {
            val edit = context.getSharedPreferences(SET, Context.MODE_PRIVATE).edit()
            edit.putString("uuid", uuid)
            edit.apply()

        }

        fun getUUID(context: Context) = context.getSharedPreferences(SET, Context.MODE_PRIVATE).getString("uuid", "")


        fun clear(context: Context) {
            val edit = context.getSharedPreferences(USER, Context.MODE_PRIVATE).edit()
            edit.clear()
            edit.apply()

        }


        fun saveIsTui(context: Context, b: Boolean) {
            val edit = context.getSharedPreferences(SET, Context.MODE_PRIVATE).edit()
            edit.putBoolean("isGetTui", b)
            edit.apply()
        }

        @JvmStatic
        fun getTui(context: Context) =
            context.getSharedPreferences(SET, Context.MODE_PRIVATE).getBoolean("isGetTui", true)

        @JvmStatic
        fun saveDeviceToken(context: Context, deviceToken: String) {
            val edit = context.getSharedPreferences(SET, Context.MODE_PRIVATE).edit()
            edit.putString("umToken", deviceToken)
            edit.apply()

        }

        fun getUmToken(context: Context) =
            context.getSharedPreferences(SET, Context.MODE_PRIVATE).getString("umToken", "")


        /**
         * 1 为分享  2 为登陆
         */
        fun saveIsLoginOrShare(context: Context, i: Int) {
            val edit = context.getSharedPreferences(SET, Context.MODE_PRIVATE).edit()
            edit.putInt("isLoginOrShare", i)
            edit.apply()

        }


        /**
         * 1 为分享  2 为登陆
         */
        fun getIsLoginOrShare(context: Context) =
            context.getSharedPreferences(SET, Context.MODE_PRIVATE).getInt("isLoginOrShare", 1)

        /**
         * 保存经纬度
         */
        fun saveLatLng(context: Context, location: Location?) {
            val edit = context.getSharedPreferences(SET, Context.MODE_PRIVATE).edit()
            edit.putFloat("lat", location!!.latitude.toFloat())
            edit.putFloat("lng", location.longitude.toFloat())
            edit.apply()
        }


        fun getLocation(context: Context): Location {
            val location = Location("")
            val share = context.getSharedPreferences(SET, Context.MODE_PRIVATE)
            location.latitude = share.getFloat("lat", 0f).toDouble()
            location.longitude = share.getFloat("lng", 0f).toDouble()
            return location

        }

        /**
         * 保存请求的 sessionId
         */
        fun saveSessionId(context: Context, sessionid: String?) {
            val edit = context.getSharedPreferences(SET, Context.MODE_PRIVATE).edit()
            edit.putString("sessionid",sessionid)
            edit.apply()

        }

        fun getSessionId(context: Context)=context.getSharedPreferences(SET,Context.MODE_APPEND).getString("sessionid","")

    }
}